package org.opens.javaskill.jvm.classloader;

/**
 * 说明:
 * <pre>
 *     1. 两个类中因为变量的位置不同导致的输出不一致问题从classloader的`链接, 初始化`阶段进行分析.
 * </pre>
 * @Date 2020/10/25 12:31
 * @Created by 99126
 */
public class LinkAndInitialization {

    public static int x = 10;

    public static int y;

    public static LinkAndInitialization linkAndInitialization = new LinkAndInitialization();

    private LinkAndInitialization() {
        LinkAndInitialization.x++;
        LinkAndInitialization.y++;
    }

    public static LinkAndInitialization getInstance() {
        return linkAndInitialization;
    }

    /**
     * 输出:
     * <pre>
     *  11
     *  1
     * </pre>
     */
    public static void main(String[] args) {
        getInstance();
        System.out.println(LinkAndInitialization.x);
        System.out.println(LinkAndInitialization.y);
    }

}

class LinkAndInitialization2 {

    public static LinkAndInitialization2 linkAndInitialization = new LinkAndInitialization2();

    public static int x = 10;

    public static int y;

    private LinkAndInitialization2() {
        LinkAndInitialization2.x++;
        LinkAndInitialization2.y++;
    }

    public static LinkAndInitialization2 getInstance() {
        return linkAndInitialization;
    }

    /**
     * 输出:
     * <pre>
     *  10
     *  1
     * </pre>
     */
    public static void main(String[] args) {
        getInstance();
        System.out.println(LinkAndInitialization2.x);
        System.out.println(LinkAndInitialization2.y);
    }

}